﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using QDT.Common.Data;
using QDT.Core.Domain;
using QDT.MVC.Security;
using QDT.Web.Admin.Helper;
using QDT.Web.Admin.Models;
using System.Web.WebPages;

namespace QDT.Web.Admin.Controllers {
    [SecurityModule(Name = "文章分类管理")]
    public class ArticleCateController : BaseController {
        //
        // GET: /ArticleCate/

        [SecurityNode(Name = "文章分类列表")]
        public ActionResult Index(GridSortOptions sort) {
            ViewData["sort"] = sort;
            List<ArticleCate> data =
                DbProvider.D<ArticleCate>().Where(
                    o => (o.ParentId == null || o.ParentId.Value == 0) && !o.IsDelete).OrderBy(
                        sort.Column, sort.Direction == SortDirection.Ascending).ToList();
            IList<ArticleCateModel> model = data.ToModelList();
            return View(model.ToTreeNode());
        }

        [SecurityNode(Name = "文章回收站")]
        public ActionResult Trash(GridSortOptions sort) {
            ViewData["sort"] = sort;
            List<ArticleCate> data =
                DbProvider.D<ArticleCate>().Where(
                    o => o.IsDelete).OrderBy(
                        sort.Column, sort.Direction == SortDirection.Ascending).ToList();
            IList<ArticleCateModel> model = data.ToModelList();
            return View(model);
        }

        [HttpGet]
        [SecurityNode(Name = "新增页面")]
        public ActionResult Add() {
            List<ArticleCate> data =
                DbProvider.D<ArticleCate>().Where(
                    o => (o.ParentId == null || o.ParentId.Value == 0) && o.IsEffect && !o.IsDelete).ToList();
            IList<ArticleCateModel> model = data.ToModelList();
            return View(model);
        }

        [HttpPost]
        [SecurityNode(Name = "新增")]
        public ActionResult PostAdd(ArticleCateModel model) {
            ArticleCate entity = model.ToEntity();
            DbProvider.Add(entity);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }

        [SecurityNode(Name = "编辑页面")]
        public ActionResult Edit(int id) {
            var data = DbProvider.GetByID<ArticleCate>(id);
            var model = new ArticleCateModel(data);
            List<ArticleCate> cate =
                DbProvider.D<ArticleCate>().Where(
                    o => (o.ParentId == null || o.ParentId.Value == 0) && o.IsEffect && !o.IsDelete).ToList();
            IList<ArticleCateModel> cateModel = cate.ToModelList();
            ViewData["CateModel"] = cateModel.ToTreeNode();
            return View(model);
        }

        [HttpPost]
        [SecurityNode(Name = "编辑")]
        public ActionResult PostEdit(ArticleCateModel model) {
            var entity = DbProvider.GetByID<ArticleCate>(model.Id);
            ArticleCate data = model.ToEntity();
            entity.Title = data.Title;
            entity.Brief = data.Brief;
            entity.ParentId = data.ParentId;
            entity.TypeId = data.TypeId;
            entity.IsEffect = data.IsEffect;
            entity.Sort = data.Sort;
            DbProvider.Update(entity);
            DbProvider.SaveChanges();
            return Success("操作成功");
        }

        [HttpPost]
        [SecurityNode(Name = "删除")]
        public JsonResult Delete(int[] id) {
            try {
                if (id.Length == 0) {
                    throw new Exception("请选择要删除的项");
                }
                foreach (int i in id) {
                    var data = DbProvider.GetByID<ArticleCate>(i);
                    if (data.Child.Count > 0) {
                        throw new Exception("请先删除子分类");
                    }
                    Article article = DbProvider.D<Article>().FirstOrDefault(o => o.CateId == i && !o.IsDelete);
                    if (article != null) {
                        throw new Exception("请先删除文章");
                    }
                    data.IsDelete = true;
                    DbProvider.Update(data);
                }
                DbProvider.SaveChanges();
                return Json(new {
                    Status = true, Info = "操作成功"
                });
            }
            catch (Exception ex) {
                return Json(new {
                    Status = false, Info = ex.Message
                });
            }
        }

        [HttpPost]
        [SecurityNode(Name = "彻底删除")]
        public JsonResult ForeverDel(int[] id) {
            try {
                if (id.Length == 0) {
                    throw new Exception("请选择要删除的项");
                }
                foreach (int i in id) {
                    int tmpId = i;
                    DbProvider.Delete<ArticleCate>(o => o.ID == tmpId);
                }
                DbProvider.SaveChanges();
                return Json(new {
                    Status = true, Info = "操作成功"
                });
            }
            catch (Exception ex) {
                return Json(new {
                    Status = false, Info = ex.Message
                });
            }
        }

        [HttpPost]
        [SecurityNode(Name = "恢复")]
        public JsonResult Restore(int[] id) {
            try {
                if (id.Length == 0) {
                    throw new Exception("请选择要删除的项");
                }
                foreach (int i in id) {
                    var data = DbProvider.GetByID<ArticleCate>(i);
                    data.IsDelete = false;
                    DbProvider.Update(data);
                }
                DbProvider.SaveChanges();
                return Json(new {
                    Status = true, Info = "操作成功"
                });
            }
            catch (Exception ex) {
                return Json(new {
                    Status = false, Info = ex.Message
                });
            }
        }
    }
}